package com.jywl.util;

import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class CommUtil {
	public static String UUid() {
		return (UUID.randomUUID().toString().replaceAll("-", ""));
	}

	/**
	 * MD5加密
	 * 
	 * @param plainText
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String HEXAndMd5(String plainText) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(plainText.getBytes("UTF8"));
		byte b[] = md.digest();
		int i;
		StringBuffer buf = new StringBuffer(200);
		for (int offset = 0; offset < b.length; offset++) {
			i = b[offset] & 0xff;
			if (i < 16) {
				buf.append("0");
			}
			buf.append(Integer.toHexString(i));
		}
		return buf.toString();
	}

	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
		return uuid.replaceAll("-", "");
	}

	public static final int getProcessID() {
		RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
		System.out.println(runtimeMXBean.getName());
		return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
	}
}
